package com.laicunba.service.impl;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.laicunba.constant.GloabConstant;
import com.laicunba.pojo.BorrowerDO;
import com.laicunba.service.BorrowerService;
import com.laicunba.service.jzh.SecurityUtils;
import com.laicunba.util.AssertUtil;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.HttpUtil;
import com.laicunba.util.MD5Util;
import com.laicunba.util.StringUtil;
import com.laicunba.util.XmlUtil;

@Service
public class BorrowerServiceImpl extends BaseServiceImpl implements BorrowerService {
	private static final char SPLIT_CHAR = '|';

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public boolean createJzhArtiAccount(BorrowerDO borrowerDO) {
		long seq = CacheUtil.incr("sys.incr.create.jzh.artiaccount", 3600);
		String mchntTxnSsn = Long.toString(System.currentTimeMillis()) + StringUtil.randomNumber(3) + String.format("%03d", seq);
		borrowerDO.setJzhLPassword(MD5Util.md5Hex(borrowerDO.getJzhLPassword()));
		borrowerDO.setJzhPassword(MD5Util.md5Hex(borrowerDO.getJzhPassword()));
		borrowerDO.setMchntTxnSsn(mchntTxnSsn);
		StringBuilder src = new StringBuilder();
		src.append(borrowerDO.getCorporateName()).append(SPLIT_CHAR);
		src.append(borrowerDO.getBankName()).append(SPLIT_CHAR);
		src.append(borrowerDO.getCardNo()).append(SPLIT_CHAR);
		src.append(borrowerDO.getPid()).append(SPLIT_CHAR);
		src.append(borrowerDO.getCityId()).append(SPLIT_CHAR);
		src.append(borrowerDO.getCompanyName()).append(SPLIT_CHAR);
		src.append(borrowerDO.getEmail()).append(SPLIT_CHAR);
		src.append(borrowerDO.getJzhLPassword()).append(SPLIT_CHAR);
		src.append(GloabConstant.JZH_MCHNT_CD).append(SPLIT_CHAR);
		src.append(mchntTxnSsn).append(SPLIT_CHAR);
		src.append(borrowerDO.getMobile()).append(SPLIT_CHAR);
		src.append(borrowerDO.getParentBankId()).append(SPLIT_CHAR);
		src.append(borrowerDO.getJzhPassword()).append(SPLIT_CHAR);
		src.append("").append(SPLIT_CHAR);
		src.append("0.44");
		borrowerDO.setId(mchntTxnSsn);

		String signature = SecurityUtils.sign(src.toString());
		Map param = new HashMap();
		param.put("ver", "0.44");
		param.put("mchnt_cd", GloabConstant.JZH_MCHNT_CD);
		param.put("mchnt_txn_ssn", mchntTxnSsn);
		param.put("cust_nm", borrowerDO.getCompanyName());
		param.put("artif_nm", borrowerDO.getCorporateName());
		param.put("certif_id", borrowerDO.getPid());
		param.put("mobile_no", borrowerDO.getMobile());
		param.put("email", borrowerDO.getEmail());
		param.put("city_id", borrowerDO.getCityId());
		param.put("parent_bank_id", borrowerDO.getParentBankId());
		param.put("bank_nm", borrowerDO.getBankName());
		param.put("capAcntNo", borrowerDO.getCardNo());
		param.put("password", borrowerDO.getJzhPassword());
		param.put("lpassword", borrowerDO.getJzhLPassword());
		param.put("rem", "");
		param.put("signature", URLEncoder.encode(signature));
		System.out.println(param);
		String xmlResult = null;
		try {
			xmlResult = HttpUtil.doPost(GloabConstant.JZH_MCHNT_URL + "/artifReg.action", param);
			System.out.println(xmlResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Document doc = null;
		try {
			doc = XmlUtil.parseXml(xmlResult.getBytes());
		} catch (Exception e) {
			AssertUtil.assertFail("XML_ERROR:解析结果错误");
		}
		String respCode = XmlUtil.getValue(doc, "resp_code");
		String respDesc = XmlUtil.getValue(doc, "resp_desc");
		AssertUtil.assertTrue("0000".equals(respCode), respCode + ":" + respDesc);
		String mchntCd = XmlUtil.getValue(doc, "mchnt_cd");
		String respMchntTxnSsn = XmlUtil.getValue(doc, "mchntTxnSsn");
		String respSignature = XmlUtil.getValue(doc, "signature");
		boolean isFromJzhServer = SecurityUtils.verifySign(String.format("%s|%s|%s|%s", respCode, respDesc, mchntCd, respMchntTxnSsn), respSignature);
		// AssertUtil.assertTrue(isFromJzhServer,
		// "VERIFY_SIGN_FAIL:结果不来自存管服务器");
		AssertUtil.assertTrue(create(borrowerDO), "CREATE_BORROWER_DO_FAIL:创建借款人出错");
		return true;
	}
	
    @Override
    public BorrowerDO getbyMobile(String mobile) {
          return queryDao.selectOne("BORROWER.getByMobile", mobile);
    }
    
    @Override
    public List<BorrowerDO> findBorrower() {
          return queryDao.selectList("BORROWER.list", null);
    }

	@Override
	public BorrowerDO get(String id) {
		return queryDao.selectOne("BORROWER.getById", id);
	}


}
